import 'package:flutter/material.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_api.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_category.dart';
import 'package:schedule_self_client/ui/components/task_category_dialog.dart';

class TaskCategoryMgmt extends StatefulWidget {
  const TaskCategoryMgmt({super.key});

  @override
  State<TaskCategoryMgmt> createState() => _TaskCategoryMgmtState();
}

class _TaskCategoryMgmtState extends State<TaskCategoryMgmt> {
  final List<TaskCategory> _taskCategoryList = [];

  @override
  void initState() {
    super.initState();
    _loadTaskCategoryList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('任务分类管理'),
      ),
      body: ListView.builder(
          itemCount: _taskCategoryList.length,
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: const EdgeInsets.all(4.0),
              child: Container(
                padding: const EdgeInsets.all(8.0),
                decoration: BoxDecoration(
                  color: Color(int.parse(_taskCategoryList[index].color)),
                  borderRadius: BorderRadius.circular(8.0),
                  border: Border.all(width: 1),
                ),
                child: Text(
                  _taskCategoryList[index].name,
                  style: Theme.of(context).textTheme.titleMedium,
                ),
              ),
            );
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          showDialog(
              context: context,
              builder: (context) => TaskCategoryDialog(okCallback: () {}));
        },
        shape: const CircleBorder(),
        child: const Icon(Icons.add),
      ),
    );
  }

  Future<void> _loadTaskCategoryList() async {
    final taskCategoryListResp = await TaskApi.getTaskCategoryList();
    setState(() {
      _taskCategoryList.addAll(taskCategoryListResp.data);
    });
  }
}
